using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace LS.Saving
{
    public static class SavingExtension
    {

        /// <summary>
        /// 获取所有游戏配置项的名称。
        /// </summary>
        /// <returns>所有游戏配置项的名称。</returns>
        public static string[] GetAllSavingNames(this SavingComponent saving, string group)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetAllNames() ?? Array.Empty<string>();
        }

        /// <summary>
        /// 获取所有游戏配置项的名称。
        /// </summary>
        /// <param name="results">所有游戏配置项的名称。</param>
        public static void GetAllSavingNames(this SavingComponent saving, string group, List<string> results)
        {
            Database db = saving.GetDatabase(group);
            db?.GetAllNames(results);
        }

        /// <summary>
        /// 检查是否存在指定游戏配置项。
        /// </summary>
        /// <param name="saving"></param>
        /// <param name="group"></param>
        /// <param name="key"></param>
        /// <returns>指定的游戏配置项是否存在。</returns>
        public static bool HasSaving(this SavingComponent saving, string group, string key)
        {
            Database db = saving.GetDatabase(group);
            return db?.HasData(key) ?? false;
        }

        /// <summary>
        /// 移除指定游戏配置项。
        /// </summary>
        /// <param name="saving"></param>
        /// <param name="group"></param>
        /// <param name="key"></param>
        /// <returns>是否移除指定游戏配置项成功。</returns>
        public static bool RemoveSaving(this SavingComponent saving, string group, string key)
        {
            Database db = saving.GetDatabase(group);
            return db?.RemoveData(key) ?? false;
        }

        /// <summary>
        /// 清空所有游戏配置项。
        /// </summary>
        public static void RemoveAllSavings(this SavingComponent saving, string group)
        {
            Database db = saving.GetDatabase(group);
            db?.RemoveAllSavings();
        }

        /// <summary>
        /// 从指定游戏配置项中读取布尔值。
        /// </summary>
        /// <param name="saving"></param>
        /// <param name="group"></param>
        /// <param name="key"></param>
        /// <param name="savingName">要获取游戏配置项的名称。</param>
        /// <returns>读取的布尔值。</returns>
        public static bool? GetBool(this SavingComponent saving, string group, string key)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetBool(key);
        }

        /// <summary>
        /// 从指定游戏配置项中读取布尔值。
        /// </summary>
        /// <param name="saving"></param>
        /// <param name="group"></param>
        /// <param name="key"></param>
        /// <param name="defaultValue">当指定的游戏配置项不存在时，返回此默认值。</param>
        /// <param name="savingName">要获取游戏配置项的名称。</param>
        /// <returns>读取的布尔值。</returns>
        public static bool GetBool(this SavingComponent saving, string group, string key, bool defaultValue)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetBool(key, defaultValue) ?? defaultValue;
        }

        /// <summary>
        /// 向指定游戏配置项写入布尔值。
        /// </summary>
        /// <param name="value">要写入的布尔值。</param>
        public static void SetBool(this SavingComponent saving, string group, string key, bool value)
        {
            Database db = saving.GetDatabase(group);
            db?.SetBool(key, value);
        }

        /// <summary>
        /// 从指定游戏配置项中读取整数值。
        /// </summary>
        /// <returns>读取的整数值。</returns>
        public static int? GetInt(this SavingComponent saving, string group, string key)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetInt(key);
        }

        /// <summary>
        /// 从指定游戏配置项中读取整数值。
        /// </summary>
        /// <param name="defaultValue">当指定的游戏配置项不存在时，返回此默认值。</param>
        /// <returns>读取的整数值。</returns>
        public static int GetInt(this SavingComponent saving, string group, string key, int defaultValue)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetInt(key, defaultValue) ?? defaultValue;
        }

        /// <summary>
        /// 向指定游戏配置项写入整数值。
        /// </summary>
        /// <param name="value">要写入的整数值。</param>
        public static void SetInt(this SavingComponent saving, string group, string key, int value)
        {
            Database db = saving.GetDatabase(group);
            db?.SetInt(key, value);
        }

        /// <summary>
        /// 从指定游戏配置项中读取浮点数值。
        /// </summary>
        /// <param name="savingName">要获取游戏配置项的名称。</param>
        /// <returns>读取的浮点数值。</returns>
        public static float? GetFloat(this SavingComponent saving, string group, string key)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetFloat(key);
        }

        /// <summary>
        /// 从指定游戏配置项中读取浮点数值。
        /// </summary>
        /// <param name="savingName">要获取游戏配置项的名称。</param>
        /// <param name="defaultValue">当指定的游戏配置项不存在时，返回此默认值。</param>
        /// <returns>读取的浮点数值。</returns>
        public static float GetFloat(this SavingComponent saving, string group, string key, float defaultValue)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetFloat(key, defaultValue) ?? defaultValue;
        }

        /// <summary>
        /// 向指定游戏配置项写入浮点数值。
        /// </summary>
        /// <param name="savingName">要写入游戏配置项的名称。</param>
        /// <param name="value">要写入的浮点数值。</param>
        public static void SetFloat(this SavingComponent saving, string group, string key, float value)
        {
            Database db = saving.GetDatabase(group);
            db?.SetFloat(key,value);
        }

        /// <summary>
        /// 从指定游戏配置项中读取字符串值。
        /// </summary>
        /// <param name="savingName">要获取游戏配置项的名称。</param>
        /// <returns>读取的字符串值。</returns>
        public static string GetString(this SavingComponent saving, string group, string key)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetString(key);
        }

        /// <summary>
        /// 从指定游戏配置项中读取字符串值。
        /// </summary>
        /// <param name="savingName">要获取游戏配置项的名称。</param>
        /// <param name="defaultValue">当指定的游戏配置项不存在时，返回此默认值。</param>
        /// <returns>读取的字符串值。</returns>
        public static string GetString(this SavingComponent saving, string group, string key, string defaultValue)
        {
            Database db = saving.GetDatabase(group);
            return db?.GetString(key, defaultValue) ?? defaultValue;
        }

        /// <summary>
        /// 向指定游戏配置项写入字符串值。
        /// </summary>
        /// <param name="savingName">要写入游戏配置项的名称。</param>
        /// <param name="value">要写入的字符串值。</param>
        public static void SetString(this SavingComponent saving, string group, string key, string value)
        {
            Database db = saving.GetDatabase(group);
            db?.SetString(key, value);
        }
    }
}
